package em.ctrl;


import base.ctrl.BaseController;
import base.jfinal.JsonUtil;
import base.jfinal.anatation.RouteBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

@RouteBind(path = "eqptOverView")
public class EqptOverViewCtrl extends BaseController {
	// 按组织统计闲置设备
	public void getIdleEqptByOrg() {
		List list = Db
				.find(" SELECT  sum(Case eqpt.Type when '1' then 1 else 0 end) 'count1', sum(Case eqpt.Type when '2' then 1 else 0 end) 'count2' "
						+ ", org.orgId ,org.name FROM em_eqpt eqpt JOIN em_org org ON eqpt.ORGID = org.ORGID JOIN em_org_relation eor ON eqpt.orgid = eor.childorgid  LEFT JOIN em_view_eqptlist ve ON eqpt.eqptId = ve.EqptId "
						+ " WHERE eor.orgid=? AND ve.`Status`='闲置' GROUP BY org.orgId ",
						this.getParaToInt("GlobalCompany"));
		JsonUtil idleList = new JsonUtil(list);
		renderJson(idleList.toString());
	}

	// 按仓库、工地统计闲置设备
	public void getIdleEqptByWh() {
		List list = Db
				.find("SELECT  sum(Case eqpt.Type when '1' then 1 else 0 end) 'count1', sum(Case eqpt.Type when '2' then 1 else 0 end) 'count2'"
						+ ", ed.CurWHId ,  iw.`Name` FROM em_eqpt eqpt LEFT JOIN em_view_eqptlist ve ON eqpt.EqptId = ve.EqptId LEFT JOIN inv_eqpt_data ed ON eqpt.eqptId = ed.EqptId LEFT JOIN inv_warehouse iw ON ed.CurWHId = iw.whId  "
						+ "LEFT JOIN em_org org ON eqpt.ORGID = org.ORGID LEFT JOIN em_org_relation eor ON eqpt.orgid = eor.childorgid WHERE eor.orgid=? AND ve.`Status`='闲置'  GROUP BY ed.CurWHId  ",
						getParaToInt("GlobalCompany"));
		JsonUtil idleList = new JsonUtil(list);
		renderJson(idleList.toString());
	}

	// 按维修人统计维修设备
	public void getMaintCountByEmp() {
		List list = Db
				.find("SELECT CASE WHEN emp.`Name` is NULL  THEN '未知' ELSE emp.`Name` END 'name' ,COUNT(m.MaintICID) 'count'"
						+ " FROM em_eqpt eqpt JOIN em_maint_log m ON eqpt.EqptId=m.EqptId  LEFT JOIN em_org o ON eqpt.OrgId=o.OrgId LEFT JOIN em_org_relation eor ON eqpt.orgid=eor.childorgid LEFT JOIN em_emp emp ON  m.MaintICID=emp.ICID "
						+ "where eor.orgid=? and m.Type='1'  GROUP BY name ",
						getParaToInt("GlobalCompany"));
		JsonUtil idleList = new JsonUtil(list);
		renderJson(idleList.toString());
	}
	/*查询设备ID,设备编号,终端编号*/
	public void getEqptCodeListByBinding(){
		int OrgId=getParaToInt("GlobalOrgId");
		List<Record> Eqpts=Db.find("SELECT el.EqptId,el.EqptCode,el.TermCode FROM em_view_eqptlist el,em_org_relation o  WHERE el.OrgId=o.ChildOrgId AND o.OrgId=?",OrgId);
		JsonUtil json=new JsonUtil(Eqpts);
		renderJson(json.toString());
	}
	public void getEmpAndIcByUnBinding(){
		int OrgId=Db.findFirst("SELECT OrgId FROM em_eqpt WHERE EqptId=?",this.getParaToInt("EqptId")).getInt("OrgId");
		List<Record> Emps=Db.find("SELECT p.ICID,p.Name FROM (SELECT ICID FROM em_eqpt_icid_map WHERE eqptId=?) AS m RIGHT JOIN em_emp p ON m.ICID=p.ICID WHERE m.ICID IS NULL AND p.Post=0 AND p.`Status`=0 AND p.ICID<>'' AND OrgId=?",this.getParaToInt("EqptId"),OrgId);
		JsonUtil json=new JsonUtil(Emps);
		renderJson(json.toString());
	}
	public void getEmpAndIcByBinding(){
		List<Record> emps=Db.find("SELECT p.`Name` FROM em_eqpt_icid_map m JOIN em_emp p ON m.icid=p.ICID JOIN em_eqpt q ON p.OrgId=q.OrgId WHERE q.eqptId=? AND (m.ICID<>NULL OR m.ICID<>'')",this.getParaToInt("EqptId"));
		JsonUtil json=new JsonUtil(emps);
		renderJson(json.toString());
	}
	public void SaveBindIc(){
		String ICs=getPara("ICID");
		int EqptId=getParaToInt("eqptId");
		String[] icarr=ICs.split(",");
		Object[][] paras = new Object[icarr.length][2];
		for(int i=0;i<icarr.length;i++){
			 Object[] para = {EqptId,icarr[i]};
			 paras[i]=para;
		}
		Db.batch("INSERT INTO em_eqpt_icid_map(eqptId,ICID) VALUES(?,?)", paras, 20);
		renderJson("{\"err\":0}");
	}
	public void ICIDUnbundling(){
		Db.deleteById("em_eqpt_icid_map","id",getParaToInt("Id"));
		renderJson("{\"err\":0}");
	}
}
